home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 486 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  40 lines

  1. Path: news.th-darmstadt.de!news!enno
  2. From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: beginner question - constants
  5. Date: 04 Jan 1996 20:07:56 GMT
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Distribution: world
  8. Message-ID: <ENNO.96Jan4210757@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  9. References: <4ceht0$ruj@sun.cis.smu.edu>
  10. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  11. In-reply-to: dbowman@post.smu.edu's message of 3 Jan 1996 12:28:48 -0600
  12.  
  13. In article <4ceht0$ruj@sun.cis.smu.edu> dbowman@post.smu.edu (Damon Bowman) writes:
  14.  
  15.    Can anyone explain to me the functional difference between a
  16.    macro-based constant and a formal constant?  I understand that by
  17.    using the #define directive, the preprocessor replaces all instances
  18.    of the constant in the code with itÆs value.  I also understand that
  19.    formal constants (using the const keyword) do not undergo this
  20.    replacement.
  21.  
  22.    What I donÆt understand is why one might be better than another and
  23.    what conditions might influence me to choose one over the other.  They
  24.    appear to be two versions of the same thing.
  25.  
  26.    Is it a speed issue?  Intuitively, the macro-based constant seems like
  27.    it would process faster.  WhatÆs the tradeoff?
  28.  
  29. Macros are globaly substituted, they don't e.g. respect namespaces and
  30. therefore may result in unwanted substitutions. This makes it hard to
  31. use them in larger projects.
  32. Constants don't share these problems but can lead to minor speed-penalities
  33. if their values must be determined at run-time.
  34. If the values can be ascertained at compile-time the resulting code shouldn't
  35. be slower or larger code compared to the code using the appropriate macro
  36. definitions.
  37.  
  38.         Enno
  39.  
  40.